package net.zretc.controller;

import net.zretc.entity.Cart;
import net.zretc.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/carts")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping
    public List<Cart> findAll() {
        try {
            return cartService.findAll();
        } catch (Exception e) {
            System.err.println("获取购物车列表出错: " + e.getMessage());
            return null;
        }
    }

    @GetMapping("/user/{userId}")
    public List<Cart> findByUserId(@PathVariable Integer userId) {
        try {
            System.out.println("查询用户ID为 " + userId + " 的购物车");
            List<Cart> cartItems = cartService.findByUserId(userId);
            System.out.println("找到 " + cartItems.size() + " 个购物车项目");
            for (Cart item : cartItems) {
                System.out.println("购物车项目: " + item);
            }
            return cartItems;
        } catch (Exception e) {
            System.err.println("获取用户购物车出错: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping
    public int insert(@RequestBody Cart cart) {
        try {
            System.out.println("添加购物车项目: " + cart);

            // 检查是否已存在相同的商品
            List<Cart> existingItems = cartService.findByUserId(cart.getUserId());
            Cart existingItem = existingItems.stream()
                    .filter(item -> item.getProductId().equals(cart.getProductId()))
                    .findFirst()
                    .orElse(null);

            if (existingItem != null) {
                // 如果已存在，更新数量
                existingItem.setQuantity(existingItem.getQuantity() + cart.getQuantity());
                int result = cartService.update(existingItem);
                System.out.println("更新购物车数量，结果: " + result);
                return result;
            } else {
                // 如果不存在，创建新项目
                if (cart.getCreateTime() == null) {
                    cart.setCreateTime(new Date());
                }
                int result = cartService.insert(cart);
                System.out.println("添加新购物车项目，结果: " + result);
                return result;
            }
        } catch (Exception e) {
            System.err.println("插入购物车项出错: " + e.getMessage());
            e.printStackTrace();
            return 0;
        }
    }

    @PutMapping
    public int update(@RequestBody Cart cart) {
        try {
            return cartService.update(cart);
        } catch (Exception e) {
            System.err.println("更新购物车项出错: " + e.getMessage());
            return 0;
        }
    }

    @DeleteMapping("/{cartId}")
    public int delete(@PathVariable Integer cartId) {
        try {
            return cartService.delete(cartId);
        } catch (Exception e) {
            System.err.println("删除购物车项出错: " + e.getMessage());
            return 0;
        }
    }
}